package com.iims.project.service.impl;

import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iims.common.constant.UserConstants;
import com.iims.common.utils.SeqNumUtils;
import com.iims.project.domain.CmisProject;
import com.iims.project.mapper.CmisProjectMapper;
import com.iims.project.service.IProjectService;
import com.iims.project.utils.ProjectCodeGenerator;
import com.iims.project.vo.query.GroupProjectSearchQuery;
import com.iims.project.vo.resp.CmisProjectDetailVo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author fanpei
 * @date 2021/7/7 14:33
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<CmisProjectMapper, CmisProject> implements IProjectService {


    @Override
    public List<CmisProjectDetailVo> getProjectList(CmisProject project) {
        List list = baseMapper.selectProjectList(project);
        return SeqNumUtils.initSeqNum(project, list);
    }

    @Override
    public List<CmisProjectDetailVo> getProjectListByGroup(GroupProjectSearchQuery groupSearchQuery) {
        groupSearchQuery.generateSqlArgs();
        List list = baseMapper.selectProjectListByGroup(groupSearchQuery);
        return SeqNumUtils.initSeqNum(groupSearchQuery, list);
    }

    @Override
    public List<CmisProjectDetailVo> getContractProjectList(Long contractId) {
        return baseMapper.selectContractProjectList(contractId);
    }

    @Override
    public CmisProjectDetailVo getProjectDetailById(Long proId) {
        return baseMapper.selectProjectDetail(proId);
    }

    @Override
    public void insertProject(CmisProject project) {
        String proCode = ProjectCodeGenerator.getProCode(project);
        project.setProCode(proCode);
        baseMapper.insert(project);
    }

    @Override
    public void updateProject(CmisProject project) {
        LambdaQueryWrapper<CmisProject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .select(CmisProject::getProCode)
                .eq(CmisProject::getProId, project.getProId());
        CmisProject OLD = baseMapper.selectOne(queryWrapper);
        //排除proCode
        project.setProCode(OLD.getProCode());
        baseMapper.updateById(project);
    }

    @Override
    public void deleteById(Long projectId) {
        baseMapper.deleteById(projectId);
    }

    @Override
    public String checkProNameUnique(CmisProject project) {
        Long proId = Validator.isNull(project.getProId()) ? -1L : project.getProId();
        CmisProject info = getOne(new LambdaQueryWrapper<CmisProject>()
                .eq(CmisProject::getProName, project.getProName()).last("limit 1"));
        if (Validator.isNotNull(info) && info.getProId().longValue() != proId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public boolean checkCmisProjectUse(Long projectId) {
        return baseMapper.selectProjectUseCount(projectId) > 0;
    }
}
